package com.cmict.data.bpm.service.definition;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cmict.data.bpm.controller.admin.definition.vo.form.ExternalFormCreateReqVO;
import com.cmict.data.bpm.controller.admin.definition.vo.form.ExternalFormExportReqVO;
import com.cmict.data.bpm.controller.admin.definition.vo.form.ExternalFormPageReqVO;
import com.cmict.data.bpm.controller.admin.definition.vo.form.ExternalFormUpdateReqVO;
import com.cmict.data.bpm.convert.definition.ExternalFormConvert;
import com.cmict.data.bpm.dal.dataobject.definition.ExternalFormDO;
import com.cmict.data.bpm.dal.mysql.definition.ExternalFormMapper;
import com.cmict.data.form.api.task.dto.BpmColumnDTO;
import com.cmict.data.form.api.task.dto.FormBaseDTO;
import com.cmict.data.framework.common.pojo.PageResult;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.cmict.data.bpm.enums.ErrorCodeConstants.EXTERNAL_FORM_NOT_EXISTS;
import static com.cmict.data.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 工作流外部表单定义 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class ExternalFormServiceImpl implements ExternalFormService {

    @Resource
    private ExternalFormMapper externalFormMapper;

    @Override
    public Long createExternalForm(ExternalFormCreateReqVO createReqVO) {
        // 插入
        ExternalFormDO externalForm = ExternalFormConvert.INSTANCE.convert(createReqVO);
        externalFormMapper.insert(externalForm);
        // 返回
        return externalForm.getId();
    }

    @Override
    public void updateExternalForm(ExternalFormUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateExternalFormExists(updateReqVO.getId());
        // 更新
        ExternalFormDO updateObj = ExternalFormConvert.INSTANCE.convert(updateReqVO);
        externalFormMapper.updateById(updateObj);
    }

    @Override
    public void deleteExternalForm(Long id) {
        // 校验存在
        this.validateExternalFormExists(id);
        // 删除
        externalFormMapper.deleteById(id);
    }

    private void validateExternalFormExists(Long id) {
        if (externalFormMapper.selectById(id) == null) {
            throw exception(EXTERNAL_FORM_NOT_EXISTS);
        }
    }

    @Override
    public ExternalFormDO getExternalForm(Long id) {
        return externalFormMapper.selectById(id);
    }

    @Override
    public List<ExternalFormDO> getExternalFormList(Collection<Long> ids) {
        return externalFormMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ExternalFormDO> getExternalFormPage(ExternalFormPageReqVO pageReqVO) {
        return externalFormMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ExternalFormDO> getExternalFormList(ExternalFormExportReqVO exportReqVO) {
        return externalFormMapper.selectList(exportReqVO);
    }

    @Override
    public Map<Long, FormBaseDTO> getFormMap(Set<Long> formIds) {
        List<ExternalFormDO> externalFormList = getExternalFormList(formIds);
        return null;
    }

    @Override
    public List<BpmColumnDTO> getColumns(Long formId) {
        ExternalFormDO formDO = getExternalForm(formId);
        String conf = formDO.getConf();
        JSONObject formInfo = JSON.parseObject(conf);
        JSONArray fields = formInfo.getJSONArray("fields");
        List<BpmColumnDTO> bpmColumnDTOLIst = Lists.newArrayList();
        fields.forEach(item ->{
            JSONObject filed = (JSONObject)JSON.toJSON(item);
            BpmColumnDTO columnDTO = new BpmColumnDTO();
            columnDTO.setColumnName(filed.getString("id"));
            columnDTO.setColumnComment(filed.getString("name"));
            columnDTO.setRequired(filed.getBoolean("required"));
            String comment = String.format("%s_%s", columnDTO.getColumnName(), columnDTO.getColumnComment());
            columnDTO.setColumnComment(comment);
            bpmColumnDTOLIst.add(columnDTO);
        });
        return bpmColumnDTOLIst;
    }

}
